package com.shijunhao.graduation.controller;


import com.shijunhao.graduation.annotations.OperationLog;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.entity.SystemDictItem;
import com.shijunhao.graduation.domain.vo.GoodBadVo;
import com.shijunhao.graduation.enums.DictCode;
import com.shijunhao.graduation.enums.TableName;
import com.shijunhao.graduation.properties.OperationTarget;
import com.shijunhao.graduation.properties.OperationType;
import com.shijunhao.graduation.service.business.IBusinessUserGoodService;
import com.shijunhao.graduation.service.system.ISystemDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-24
 */
@RestController
@RequestMapping("/business-user-good")
@Api(tags = "点赞信息")
public class BusinessUserGoodController {

    @Resource
    private IBusinessUserGoodService userGoodService;
    @Resource
    private ISystemDictService dictService;

    @PostMapping("/note/good/{nid}")
    @ApiOperation("文章点赞")
    public ResultCode<Boolean> noteGood(@PathVariable Long nid) {
        SystemDictItem table = dictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_NOTE.getLabel());
        boolean success = userGoodService.doAction(nid, table.getDictValue(), true);
        if (success) {
            return ResultCode.success("点赞成功！！",true);
        }else{
            return ResultCode.success("取消点赞成功！！", false);
        }
    }

    @PostMapping("/note/discuss/good/{nid}")
    @ApiOperation("文章评论点赞")
    public ResultCode<Boolean> noteDiscussGood(@PathVariable Long nid) {
        SystemDictItem table = dictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_NOTE_DISCUSS.getLabel());
        boolean success = userGoodService.discussAction(nid, table.getDictValue());
        if (success) {
            return ResultCode.success("点赞成功！！",true);
        }else{
            return ResultCode.success("取消点赞成功！！", false);
        }
    }

    @PostMapping("/forum/discuss/good/{nid}")
    @ApiOperation("文章评论点赞")
    public ResultCode<Boolean> forumDiscussGood(@PathVariable Long nid) {
        SystemDictItem table = dictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_FORUM_DISCUSS.getLabel());
        boolean success = userGoodService.discussAction(nid, table.getDictValue());
        if (success) {
            return ResultCode.success("点赞成功！！",true);
        }else{
            return ResultCode.success("取消点赞成功！！", false);
        }
    }


    @PostMapping("/note/bad/{nid}")
    @ApiOperation("文章点踩")
    public ResultCode<Boolean> noteBad(@PathVariable Long nid) {
        SystemDictItem table = dictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_NOTE.getLabel());
        boolean success = userGoodService.doAction(nid, table.getDictValue(), false);
        if (success) {
            return ResultCode.success("点踩成功！！",true);
        }else{
            return ResultCode.success("取消点踩成功！！", false);
        }
    }

    @PostMapping("/forum/good/{nid}")
    @ApiOperation("论坛点赞")
    public ResultCode<Boolean> forumGood(@PathVariable Long nid) {
        SystemDictItem table = dictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_FORUM.getLabel());
        boolean success = userGoodService.doAction(nid, table.getDictValue(), true);
        if (success) {
            return ResultCode.success("点赞成功！！",true);
        }else{
            return ResultCode.success("取消点赞成功！！", false);
        }
    }

    @PostMapping("/forum/bad/{nid}")
    @ApiOperation("论坛点踩")
    public ResultCode<Boolean> forumBad(@PathVariable Long nid) {
        SystemDictItem table = dictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_FORUM.getLabel());
        boolean success = userGoodService.doAction(nid, table.getDictValue(), false);
        if (success) {
            return ResultCode.success("点踩成功！！",true);
        }else{
            return ResultCode.success("取消点踩成功！！", false);
        }
    }


    @GetMapping("/note/goodBad/{nid}")
    @ApiOperation("文章是否点赞或踩")
    public ResultCode<GoodBadVo> noteGoodBad(@PathVariable Long nid) {
        SystemDictItem table = dictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_NOTE.getLabel());
        GoodBadVo goodBadInfo = userGoodService.getGoodBadInfo(table.getDictValue(), nid);
        return ResultCode.success(goodBadInfo);
    }

    @GetMapping("/forum/goodBad/{nid}")
    @ApiOperation("论坛是否点赞或踩")
    public ResultCode<GoodBadVo> forumGoodBad(@PathVariable Long nid) {
        SystemDictItem table = dictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_FORUM.getLabel());
        GoodBadVo goodBadInfo = userGoodService.getGoodBadInfo(table.getDictValue(), nid);
        return ResultCode.success(goodBadInfo);
    }
}
